using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mgh.Data.Sql;
using Jewelry.BussinessLayer;
using Jewelry.Entities;

public partial class Pages_AEditCategory : Mgh.UI.Sql.PageBase
{
    #region properties

    private string CateID
    {
        get { return Request.QueryString["cateid"] == null ? string.Empty : Request.QueryString["cateid"]; }
    }

    #endregion

    #region events

    override protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(CateID))
                this.bindData();
        }
    }

    protected void btSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DataSources.Jw_Tbl_CategoryDataTable tb = this.prepairData();
            CategoryBLL bn = new CategoryBLL(getCurrentConnect());
            if (string.IsNullOrEmpty(this.CateID))
            {
                if (!bn.addCategories(tb))
                {
                    cval.IsValid = false;
                    cval.Text = bn.getMessage();
                }
                else Response.Redirect("AEditCategory.aspx");
            }
            else
            {
                if (!bn.updateCateogries(tb))
                {
                    cval.IsValid = false;
                    cval.Text = bn.getMessage();
                }
                else Response.Redirect("AManageCategory.aspx");
            }
        }
    }

    #endregion

    #region functions

    private DataSources.Jw_Tbl_CategoryDataTable prepairData()
    {
        DataSources.Jw_Tbl_CategoryDataTable tb = new DataSources.Jw_Tbl_CategoryDataTable();
        DataSources.Jw_Tbl_CategoryRow row = tb.NewJw_Tbl_CategoryRow();
        row.CateID = this.CateID;
        row.Ten = txtName.Text;
        row.Mota = edDesc.Text;
        tb.AddJw_Tbl_CategoryRow(row);
        return tb;
    }

    private void bindData()
    {
        CategoryBLL bn = new CategoryBLL(getCurrentConnect());
        DataSources.Jw_Tbl_CategoryDataTable tb = bn.getCategory(this.CateID);
        if (tb != null)
        {
            txtName.Text = tb[0].IsTenNull() ? string.Empty : tb[0].Ten;
            edDesc.Text = tb[0].IsMotaNull() ? string.Empty : tb[0].Mota;
            //txtDesc.Text = tb[0].IsMotaNull() ? string.Empty : tb[0].Mota;
        }
        else
        {
            cval.IsValid = false;
            cval.Text = bn.getMessage();
        }

    }

    #endregion
    
}
